package com.cdsf.kennedy.user.service.impl;

import com.cdsf.common.conf.AbstractService;
import com.cdsf.common.util.Page;
import com.cdsf.kennedy.user.domain.Role;
import com.cdsf.kennedy.user.mapper.RoleMapper;
import com.cdsf.kennedy.user.service.RoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Date;
import java.util.List;

/**
 * @Author: kennedy
 * @Date: Created in 15:55 2018/2/6
 */
@Service
public class RoleServiceImpl extends AbstractService implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void insert(Role role) {
        role.setUuid(randomUUID());
        role.setCreateTime(new Date());
        roleMapper.insert(role);
    }

    @Override
    public Role selectOne(Role role) {
        Role result = roleMapper.selectOne(role);
        return result;
    }

    public List<Role> getAll(Role role, Page page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        Weekend<Role> weekend = Weekend.of(Role.class);
        WeekendCriteria<Role, Object> criteria = weekend.weekendCriteria();
        if (role.getRoleName() != null) {
            criteria.andLike(Role::getRoleName, "%" + role.getRoleName() + "%");
        }
        return roleMapper.selectByExample(weekend);
    }

    @Override
    public void delete(String uuid) {
        roleMapper.deleteByPrimaryKey(uuid);
    }

    @Override
    public Role selectByPrimaryKey(String uuid) {
        return roleMapper.selectByPrimaryKey(uuid);
    }

    @Override
    public void updateByPrimaryKey(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }
}
